#include "LogaritmN.h"
#include "Impartit.h"

long double LogaritmN::calcul(long double aux1)
{
    if (aux1 <= 1e-100)
    {
       std::cout<<"Nu se poate calcula logaritmul unui numar negativ...";
        return 0;
    }

    return logl(aux1);
}

long double LogaritmN::calcul()
{
    long double aux;

    if (!fStang)
        exit(1);

    return calcul(fStang->calcul());
}

char *LogaritmN::derivare()
{
    char *aux, *aux1, *aux11;

    if (!fStang)
        exit(1);

    aux1 = fStang->derivare();
    aux11 = fStang->toString();

    aux = Impartit::concatTermeni(aux1, aux11);
    delete aux1; delete aux11;

    return aux;
}

long double LogaritmN::calculDerivata()
{
    long double aux;

    if (!fStang)
        exit(1);

    aux = fStang->calcul();
    if (ISZERO(aux))
    {
       std::cout<<"Division by 0...\n";
        return 0;
    }

    return Impartit::calcul(fStang->calculDerivata(), aux);
}

char *LogaritmN::toString()
{
    char *aux1, *aux;

    if (!fStang )
        exit(1);

    aux1 = fStang->toString();

    aux = concatTermeni(aux1);
    delete aux1;

    return aux;
}

char *LogaritmN::concatTermeni(char *aux1)
{
    char *aux;

    if (!strcmp(aux1, "1")) aux = cpystr((char*)"0");
    else
    {
        aux = new char[strlen(aux1) + 7];
        strcpy(aux, "(ln(");
        strcat(aux, aux1); strcat(aux, "))");
    }

    return aux;
}
